package common.intercepter;

import common.beans.User;
import common.util.CacheUtil;
import common.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class ExampleInterceptor implements HandlerInterceptor {

    @Resource
    HttpSession session;
    @Resource
    CacheUtil cacheUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        log.info("添加假用户测试");
        session.setAttribute("uid", "50177");
        session.setAttribute("gid", "1");
        User user = new User();
        user.setUid(50177);
        user.setGid(1);
        user.setRealname("张三");
        user.setUnitName("泉方");
        UserUtil.setUser(user);
        UserUtil.getUserUnloginAccess().setUnitName("张三");
        UserUtil.getUserUnloginAccess().setUnitName("泉方");
        cacheUtil.saveObject("user", user);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        UserUtil.clearAllUserInfo();
    }
}